﻿using AutoMapper;
using Sharp.DataTransferObject;
using Sharp.Domain.PlatformBoundedContext.PermissionModule.Aggregates;
using Sharp.Domain.PlatformBoundedContext.UserModule.Aggregates;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Search;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Sharp.Application.Platform
{
    public partial class UserServiceImpl
    {
        /// <summary>
        ///     根据指定的ID值，获取角色。
        /// </summary>
        /// <param name="id">指定的角色ID值。</param>
        /// <returns>角色。</returns>
        public RoleDTO GetRoleByKey(int id)
        {
            return Mapper.Map<Role, RoleDTO>(_roleRepository.Get(id, x => x.AuthorizedOrgs));
        }

        /// <summary>
        ///     获取所有角色。
        /// </summary>
        /// <returns>所有角色。</returns>
        public IList<RoleDTO> GetRoles()
        {
            var roles = _roleRepository.GetList();

            return roles.To<Role, RoleDTO>().ToList();
        }

        /// <summary>
        ///     创建角色。
        /// </summary>
        /// <param name="roleDTO">需要创建的角色。</param>
        /// <returns>已创建的角色。</returns>
        public Result SaveRole(RoleDTO roleDTO)
        {
            if (roleDTO == null)
                throw new ArgumentNullException(nameof(roleDTO));

            var res = new Result();
            var role = roleDTO.To<Role>();
            if (role.Id.IsEmpty())
            {
                _roleRepository.Add(role);
            }
            else
            {
                var info = _roleRepository.Get(roleDTO.Id);

                info.Remark = roleDTO.Remark;
                info.Name = roleDTO.Name;

                _roleRepository.Update(info);
            }

            Context.Commit();
            return res;
        }


        /// <summary>
        ///     删除角色。
        /// </summary>
        /// <param name="roleIDs">需要删除的角色ID值列表。</param>
        public Result DeleteRoles(IList<int> roleIDs)
        {
            var res = new Result();
            if (roleIDs == null)
            {
                res.Err = "角色Id参数不可为空！";
                return res;
            }

            _roleRepository.Remove(roleIDs);

            Context.Commit();

            return res;
        }

        /// <summary>
        ///     将指定的用户赋予指定的角色。
        /// </summary>
        /// <param name="userIds"></param>
        /// <param name="roleId">需要向用户赋予的角色ID值。</param>
        public Result AssignRole(int[] userIds, int roleId)
        {
            return _domainService.AssignRole(userIds, roleId);
        }

        /// <summary>
        /// 获取Role分页数据
        /// </summary>
        /// <param name="pf"></param>
        /// <returns></returns>
        public PagedResult<RoleDTO> GetRolePaged(PagingFeature pf)
        {
            var paged = _roleRepository.Pagging(pf, x => true, x => x.Org);

            return paged.To<Role, RoleDTO>();
        }
        public Task<IEnumerable<PermissionDTO>> GetPermissionByRoleId(int roleId)
        {
            if (roleId.IsEmpty())
            {
                throw new NullReferenceException("参数groupId不能为空。");
            }
            return Task.Run(() =>
            {
                return _roleRepository.GetPermissionByRoleId(roleId).To<Permission, PermissionDTO>();
            });
        }
    }
}
